<template>
  <el-dialog :close-on-click-modal="false" :title="form.id ? $t('common.editBtn') : $t('common.addBtn')" width="600"
             draggable v-model="visible">
   <el-form :model="form"  ref="dataFormRef" label-width="100px" :rules="dataRules" v-loading="loading">
               <el-form-item :label="$t('aigcKnowledge.name')" prop="name">
                   <el-input v-model="form.name" :placeholder="$t('aigcKnowledge.inputNameTip')" clearable ></el-input>
               </el-form-item>
               <el-form-item :label="$t('aigcKnowledge.cover')" prop="cover">
                   <el-input v-model="form.cover" :placeholder="$t('aigcKnowledge.inputCoverTip')" clearable ></el-input>
               </el-form-item>
               <el-form-item :label="$t('aigcKnowledge.embedModelId')" prop="embedModelId">
                    <el-select v-model="form.embedModelId" :placeholder="$t('aigcKnowledge.inputEmbedModelIdTip')">
                        <el-option v-for="item in embedStoreModels" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
               <el-form-item :label="$t('aigcKnowledge.embedStoreId')" prop="embedStoreId">
                    <el-select v-model="form.embedStoreId" :placeholder="$t('aigcKnowledge.inputEmbedStoreIdTip')">
                        <el-option v-for="item in embedStores" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
               </el-form-item>
               <el-form-item :label="$t('aigcKnowledge.remark')" prop="remark">
                   <el-input type="textarea" :rows="5" v-model="form.remark" :placeholder="$t('aigcKnowledge.inputRemarkTip')" clearable/>
               </el-form-item>
           </el-form>

         <template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
				<el-button @click="onSubmit" type="primary" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
			</span>
        </template>
  </el-dialog>
</template>

<script lang="ts" name="aigcKnowledgeChatDialog" setup>
import {useMessage} from '/@/hooks/message';
import {addObj, getObj, putObj } from '/@/api/aigc/knowledge';
import {useI18n} from 'vue-i18n';
import {findList as findEmbedStoreList} from "/@/api/aigc/embedStore";
import {findList as findEmbedStoreModelList} from "/@/api/aigc/model";
import {ModelTypeEnum} from "/@/types/aigc/chat";
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const {t} = useI18n();

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
//向量数据库
const embedStores = ref<any>([]);
//向量模型
const embedStoreModels = ref<any>([]);
// 提交表单数据
const form = reactive({
      id:'',
      name:'',
      cover:'',
      embedStoreId:'',
      embedModelId:'',
      remark:'',
});

// 定义校验规则
const dataRules = ref({
    name: [
        {required: true, message: '别名不能为空', trigger: 'blur'},
        {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'},
    ],
    embedStoreId: [
      {required: true, message: '向量模型不能为空', trigger: 'blur'},
    ],
    embedModelId: [
      {required: true, message: '向量存储不能为空', trigger: 'blur'},
    ],
});

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true;
  form.id = '';

  nextTick(() => {
    dataFormRef.value.resetFields();
  });

  // 获取信息
  if (id) {
    form.id = id;
    getData(id);
  }
  //初始化数据
  initData();
};
//初始化
const initData=()=>{
  //向量数据库列表获取
    findEmbedStoreList({}).then(res => {
        if(res.success){
            embedStores.value = res.data;
        }
    });
    //向量模型列表获取
    findEmbedStoreModelList(ModelTypeEnum.EMBEDDING).then(res => {
        if(res.success){
            embedStoreModels.value = res.data;
        }
    });
}
// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(t(form.id ? 'common.editSuccessText' : 'common.addSuccessText'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// 初始化数据
const getData = (id: string) => {
  getObj(id).then((res: any) => {
    Object.assign(form, res.data);
  });
};
// 暴露变量
defineExpose({
  openDialog,
});
</script>